﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HJ212_Common;

namespace HJT212_Protocol_WebServer
{
    public partial class IndexStart : System.Web.UI.Page
    {
        #region PageLoad
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Response.Cache.SetNoStore();
                showMsg();
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        #endregion

        #region 字段属性
        #endregion

        #region 事件

        protected void btnStartSrv_Click(object sender, EventArgs e)
        {
            try
            {
                string WaitTimeOut = ConfigOprater.GetNodeValue("WaitTimeOut");
                string RetryCount = ConfigOprater.GetNodeValue("RetryCount");
                string LoginTimeOut = ConfigOprater.GetNodeValue("LoginTimeOut");
                Global.server.SendTimes = Convert.ToInt32(RetryCount);
                Global.server.SendTimeOut = Convert.ToInt32(WaitTimeOut);
                Global.server.LoginTimeOut = Convert.ToInt32(LoginTimeOut);
                Global.server.Init();
                Global.server.Start();
                var url = HttpContext.Current.Request.Url;
                Response.Redirect(url.AbsolutePath);
            }
            catch (Exception ex)
            {

                throw;
            }
        }

        protected void btnStopSrv_Click(object sender, EventArgs e)
        {
            try
            {

                Global.server.Stop();
                var url = HttpContext.Current.Request.Url;
                Response.Redirect(url.AbsolutePath);

                //this.btnSrvStart.Enabled = false;
            }
            catch (Exception ex)
            {

                throw;
            }
        }


        #endregion

        #region 方法

        private void showMsg()
        {
            this.btnStartSrv.Enabled = !Global.server.IsRuning;
            this.btnStop.Enabled = Global.server.IsRuning;
            if (Global.server.IsRuning)
            {
                this.labMsg.ForeColor = Color.Red;
                string _port = ConfigOprater.GetNodeValue("SrvPort");
                this.labMsg.Text = "服务已经开启或者端口: 【" + _port + "】 被占用";
            }
            else
            {
                this.labMsg.ForeColor = Color.Green;
                this.labMsg.Text = "服务未启动";
            }
        }

        #endregion
    }
}